﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;

namespace GoShopping.BLL
{
    /// <summary>
    /// 商品图片操作类
    /// </summary>
    public class ProductImgBean
    {
        /// <summary>
        /// 默认的图片
        /// </summary>
        static readonly string _DefaultProductImagePath = System.AppDomain.CurrentDomain.BaseDirectory + @"Images\ProductImg\defaultprdct.png";

        /// <summary>
        /// 获取商品的图片路径
        /// </summary>
        /// <param name="productid"></param>
        /// <param name="productTypeId"></param>
        /// <returns>如果没有图片，则返回默认的图片</returns>
        public static string GetProductImgPath(int productid, int productTypeId)
        {
            string fileName = _DefaultProductImagePath;
            string thisPath = System.AppDomain.CurrentDomain.BaseDirectory + "Images\\ProductImg\\" + productTypeId.ToString();
            if (Directory.Exists(thisPath))
            {
                string[] arr = Directory.GetFiles(thisPath);
                foreach (string s in arr)
                {
                    FileInfo fi = new FileInfo(s);
                    string[] tm = fi.Name.Split('.');
                    if (tm[0] == productid.ToString())
                    {
                        fileName = s;
                    }
                }
            }
            return fileName;
        }

        /// <summary>
        /// 获取商品的图片路径
        /// </summary>
        /// <param name="productid"></param>
        /// <param name="productTypeId"></param>
        /// <returns>如果没有图片，则返回默认的图片</returns>
        public static IList<string> GetProductImgPathList(int productid, int productTypeId)
        {
            IList<string> fileList = new List<string>();
            string thisPath = System.AppDomain.CurrentDomain.BaseDirectory + "Images\\ProductImg\\" + productTypeId.ToString();
            if (Directory.Exists(thisPath))
            {
                string[] arr = Directory.GetFiles(thisPath);
                foreach (string s in arr)
                {
                    FileInfo fi = new FileInfo(s);
                    string[] tm = fi.Name.Split('.');
                    if (tm[0].StartsWith(productid.ToString()))
                    {
                        fileList.Add(s);
                    }
                }
            }
            return fileList;
        }

        /// <summary>
        /// 保存商品的图片
        /// </summary>
        /// <param name="productid"></param>
        /// <param name="producttypeid"></param>
        /// <param name="fu"></param>
        public static void SetProductImage(int productid, int producttypeid, FileUpload fu)
        {
            string thisPath = System.AppDomain.CurrentDomain.BaseDirectory + "Images\\ProductImg\\" + producttypeid.ToString();
            if (!Directory.Exists(thisPath))
            {
                Directory.CreateDirectory(thisPath);
            }
            if (fu.PostedFile == null)
            {
                return;
            }
            string[] arr = Directory.GetFiles(thisPath);
            foreach (string s in arr)
            {
                FileInfo fi = new FileInfo(s);
                string[] tm = fi.Name.Split('.');
                if (tm[0] == productid.ToString())
                {
                    File.Delete(s);
                }
            }
            string fileSuffix;
            string sfn = fu.FileName;
            string[] arrsfn = sfn.Split('.');
            fileSuffix = arrsfn[1];

            string fileName = thisPath + "\\" + productid.ToString() + "." + fileSuffix;
            // 取得上传的文件对象
            HttpPostedFile hpf = fu.PostedFile;
            if (fu.PostedFile != null && fu.HasFile)
            {
                fu.SaveAs(fileName);
            }
        }

        /// <summary>
        /// 保存商品的图片
        /// </summary>
        /// <param name="productid"></param>
        /// <param name="producttypeid"></param>
        /// <param name="fu"></param>
        public static void SetProductImage(int productid, int producttypeid, IList<FileUpload> fuList)
        {
            string thisPath = System.AppDomain.CurrentDomain.BaseDirectory + "Images\\ProductImg\\" + producttypeid.ToString();
            if (!Directory.Exists(thisPath))
            {
                Directory.CreateDirectory(thisPath);
            }
            string[] arr = Directory.GetFiles(thisPath);
            foreach (string s in arr)
            {
                FileInfo fi = new FileInfo(s);
                string[] tm = fi.Name.Split('.');
                if (tm[0] != productid.ToString() && tm[0].StartsWith(productid.ToString()))
                {
                    File.Delete(s);
                }
            }
            int i = 0;
            foreach (FileUpload fu in fuList)
            {
                if (fu.PostedFile != null && fu.HasFile)
                {
                    string fileSuffix;
                    string sfn = fu.FileName;
                    string[] arrsfn = sfn.Split('.');
                    fileSuffix = arrsfn[1];

                    string fileName = thisPath + "\\" + productid.ToString() + "_" + i.ToString() + "." + fileSuffix;
                    // 取得上传的文件对象
                    HttpPostedFile hpf = fu.PostedFile;
                    fu.SaveAs(fileName);
                    i++;
                }
            }
        }
    }
}
